package adoku;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import java_cup.runtime.Symbol;
import kameleon.document.Document;
import kameleon.exception.KameleonException;
import kameleon.plugin.Analyzer;
import kameleon.plugin.SupportedOptions;
import kameleon.util.IODocument;
import kameleon.util.IOFile;
import adoku.analyze.LexicalAnalyzer;
import adoku.analyze.SyntaxAnalyzer;

/**
 * Main class.
 * 
 * <p>Launches the generation for the given file and 
 * writes the result to the given file.
 * 
 * <p>Proper usage requires three command line arguments : 
 * <pre>  -g &lt;analyzed file> &lt;result file></pre>
 * 
 * @author		Brabant Quentin, Fromentin Xavier
 * @version		1.0
 */
public class Main extends Analyzer {


	public Main() {
		super() ;
	}

	/**
	 * Main function.
	 * 
	 * @param 	args
	 * 			command line arguments
	 */
	public static void main(String[] args) {
		/* Retrieve arguments */
		String cheminFichier = args[1] ;
		String cheminEcriture = args[2] ;
		String charset = args[3] ;
		try {
			/* Analyze the given file */
			System.out.printf("~~ Analyzer DokuWiki ~~\n") ;
			SyntaxAnalyzer a = new SyntaxAnalyzer(
					new LexicalAnalyzer(new InputStreamReader(
							new FileInputStream(cheminFichier), charset))) ;
			System.out.printf("Analyzing file '%s'.\n", cheminFichier) ;
			Symbol result = a.parse() ;
			Document d = (Document) result.value ;

			/* Write the analyse's result into a file */
			System.out.print("Saving result ....\n") ;
			IODocument.writeToFile(d, cheminEcriture);
			System.out.print("File successfully analyzed.\n") ;
		} catch (Exception e) {
			//			System.out.println(e.getMessage()) ;
			System.exit(1) ;
		}// try
	}// main(String [])

	@Override
	public Document analyze(String filePath, SupportedOptions options)
			throws KameleonException {
		String charset = "UTF-8" ; //$NON-NLS-1$
		try {
			SyntaxAnalyzer a = new SyntaxAnalyzer(
					new LexicalAnalyzer(new InputStreamReader(
							new FileInputStream(filePath), charset)));
			Symbol result = a.parse() ;
			return (Document) result.value ;
		} catch (UnsupportedEncodingException e) {
			throw new KameleonException(e.getMessage()) ;
		} catch (FileNotFoundException e) {
			throw new KameleonException(e.getMessage()) ;
		} catch (Exception e) {
			throw new KameleonException(e.getMessage()) ;
		}// try
	}

	@Override
	public Document analyze(String filePath) throws KameleonException {
		// TODO Temporary
		return this.analyze(filePath, null) ;
	}

	@Override
	public void copyPicture(String targetFolder) throws KameleonException {
		String[] pictureNames = new String[]{
				"DokuWikiAnalyzer_mini.png", 
				"DokuWikiAnalyzer_gray_icon.png",
				"DokuWikiAnalyzer_icon.png"
		} ;
		for(String pictureName : pictureNames) {
			try {
				InputStream src = new BufferedInputStream(
						this.getClass().getResourceAsStream(
								String.format("/adoku/resources/%s", pictureName))) ; //$NON-NLS-1$
				OutputStream dest = new BufferedOutputStream(new FileOutputStream(String.format("%s%s%s", 
						targetFolder, File.separator, pictureName))) ;
				IOFile.copyFile(src, dest) ;
				src.close() ;
				dest.close() ;
			} catch (FileNotFoundException e) {
				//TODO Handle exception
				throw new KameleonException(e.getMessage()) ;
			} catch (IOException e) {
				throw new KameleonException(e.getMessage()) ;
			}// try
		}// for
	}

}// class Main